package com.c.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.c.entity.Member;
import com.c.service.MemberService;
import com.c.service.UserService;
import com.c.utils.MessageConstant;
import com.c.utils.PageResult;
import com.c.utils.QueryPageBean;
import com.c.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author wangxiangli
 * @create 2022-12-26-15:23
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 根据页码查找会员
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
        return memberService.findByPage(queryPageBean);
    }

    /**
     * 新增会员
     *
     * @param member
     * @return
     */
    @PreAuthorize("hasAuthority('MEMBER_ADD')")
    @RequestMapping("add")
    public Result add(@RequestBody Member member) {
        try {
            memberService.add(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    /**
     * 手机号唯一性验证
     *
     * @param member 提交的数据
     * @return 返回验证
     */
    @RequestMapping("/phoneValidator")
    public String phoneValidator(@RequestBody Member member) {
        return memberService.phoneValidator(member);
    }

    /**
     * 档案号唯一性验证
     *
     * @param member 提交的数据
     * @return 返回验证
     */
    @RequestMapping("/fileNumberValidator")
    public String fileNumberValidator(@RequestBody Member member) {
        return memberService.fileNumberValidator(member);
    }

    /**
     * 身份证号唯一性验证
     *
     * @param member 提交的数据
     * @return 返回验证
     */
    @RequestMapping("/idCardValidator")
    public String idCardValidator(@RequestBody Member member) {
        return memberService.idCardValidator(member);

    }
    /**
     * 邮箱唯一性验证
     *
     * @param member 提交的数据
     * @return 返回验证
     */
    @RequestMapping("/emailValidator")
    public String emailValidator(@RequestBody Member member) {
        return memberService.emailValidator(member);
    }
    /**
     * 根据ID查询会员信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Member member = memberService.getById(id);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }


    /**
     * 修改会员信息
     *
     * @param member
     * @return
     */
    @PreAuthorize("hasAuthority('MEMBER_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member) {
        try {
            memberService.edit(member);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    /**
     * 删除会员
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('MEMBER_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            return memberService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

}
